<template>
  <div>
    <div style="height:20px;"></div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
  </div>
</template>
<script>
import { getDownloadUrl } from '@/api/omSale/subsidy/subsidyInquiry/index.js';

export default {
  data() {
    return {
      formList: this.createdFrom()
    };
  },
  watch: {
    data: {
      handler(newName, oldName) {
        this.getDetail(newName);
      },
      deep: true
    }
  },
  props: ['data'],
  components: {},
  mounted() {
    this.getDetail(this.data);
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '发票行驶证附件'
        },

        {
          type: 'UPLOAD_FILE',
          label: '发票附件',
          fieldName: 'fpfj',
          placeholder: '请选择',
          disabled: true,
          initialValue: [],
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'UPLOAD_FILE',
          label: '行驶证附件',
          fieldName: 'xszfj',
          placeholder: '请选择',
          disabled: true,
          initialValue: [],
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        }
      ];
    },

    seacherFun() {
      this.issearch = true;
    },
    changeHandle(val) {},
    getFileName(res) {
      let patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*');
      let contentDisposition = decodeURI(res.headers['content-disposition']);
      let result = patt.exec(contentDisposition);
      let fileName = result[1];
      fileName = fileName.replace(/\"/g, '');
      return fileName;
    },
    async queryDownloadUrl(id) {
      const res = await getDownloadUrl(id);
      const blob = new Blob([res.data], {
        type: res.data.type
      });
      const fileName = this.getFileName(res);
      const url = URL.createObjectURL(blob);
      console.log(url, fileName, '你是啥1？');
      return [fileName, url];
    },
    getDetail(data) {
      console.log(data, 'data');
      if (data.enclosures && data.enclosures.length > 0) {
        let invocieArr = [];
        let drivingArr = [];
        data.enclosures.map(y => {
          if (y.enclosureType == 'invoiceAddress') {
            this.queryDownloadUrl(y.enclosureId).then(info => {
              invocieArr.push({ name: info[0], url: info[1] });
            });
          } else if (y.enclosureType == 'drivingAddress') {
            this.queryDownloadUrl(y.enclosureId).then(info => {
              drivingArr.push({ name: info[0], url: info[1] });
            });
          }
        });
        this.formList.find(x => x.fieldName == 'fpfj').initialValue = invocieArr;
        this.formList.find(x => x.fieldName == 'xszfj').initialValue = drivingArr;
      }
      for (let i in data) {
        this.formList.map(x => {
          if (x.fieldName == 'fpfj') {
          } else if (x.fieldName == 'xszfj') {
          } else if (i == x.fieldName) {
            x.initialValue = data[i];
          }
        });
      }
    }
  }
};
</script>
